import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:terminal_flutter_demo/widgets/my_dialog.dart';

class DialogPage extends StatefulWidget {
  const DialogPage({Key? key}) : super(key: key);

  @override
  _DialogPageState createState() => _DialogPageState();
}

class _DialogPageState extends State<DialogPage> {
  void _showDialog(BuildContext context) {
    showDialog(
      barrierDismissible: false, // 点击遮罩层是否隐藏，默认true
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text('提示信息'),
          content: const Text('提示内容'),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.pop(context);
                print('确定');
              },
              child: const Text('确定'),
            ),
            TextButton(
              onPressed: () {
                Navigator.pop(context);
                print('取消');
              },
              child: const Text('取消'),
            ),
          ],
        );
      },
    );
  }

  void _showSimpleDialog(BuildContext context) async {
    var result = await showDialog(
      context: context,
      builder: (context) {
        return SimpleDialog(
          title: const Text('提示信息'),
          children: [
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, '返回data1');
                print('data1');
              },
              child: const Text('data1'),
            ),
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, '返回data2');
                print('data2');
              },
              child: const Text('data2'),
            ),
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, '返回data3');
                print('data3');
              },
              child: const Text('data3'),
            ),
          ],
        );
      },
    );
    print('--------');
    print(result);
  }

  void _showModalBottomSheet(BuildContext context) async {
    var result = await showModalBottomSheet(
      context: context,
      builder: (context) {
        return Container(
          height: 300,
          color: Colors.amber,
          child: Column(
            children: [
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, '返回data1');
                  print('data1');
                },
                child: const Text('data1'),
              ),
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, '返回data2');
                  print('data2');
                },
                child: const Text('data2'),
              ),
              SimpleDialogOption(
                onPressed: () {
                  Navigator.pop(context, '返回data3');
                  print('data3');
                },
                child: const Text('data3'),
              ),
            ],
          ),
        );
      },
    );
    print('--------');
    print(result);
  }

  void _showFlutterToast() {
    Fluttertoast.showToast(
      msg: "This is Center Short Toast",
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
      timeInSecForIosWeb: 1,
      backgroundColor: Colors.red,
      textColor: Colors.white,
      fontSize: 16.0,
    );
  }

  void _showMyDialog() {
    showDialog(
      barrierDismissible: true, // 点击遮罩层是否隐藏，默认true
      context: context,
      builder: (context) {
        return MyDialog(
          title: '温馨提示',
        );
      },
    );
  }

  void _showGetXDialog() {
    // 最外层须包裹GetMaterialApp组件
    // Get.defaultDialog(title: 'title');
    Get.bottomSheet(
      Container(
        height: 300,
        color: Colors.amber,
        child: Column(
          children: [
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, '返回data1');
                print('data1');
              },
              child: const Text('data1'),
            ),
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, '返回data2');
                print('data2');
              },
              child: const Text('data2'),
            ),
            SimpleDialogOption(
              onPressed: () {
                Navigator.pop(context, '返回data3');
                print('data3');
              },
              child: const Text('data3'),
            ),
          ],
        ),
      ),
      backgroundColor: Colors.blue,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          ElevatedButton(
            onPressed: () {
              _showDialog(context);
            },
            child: const Text('showDialog'),
          ),
          const SizedBox(height: 20),
          ElevatedButton(
            onPressed: () {
              _showSimpleDialog(context);
            },
            child: const Text('showSimpleDialog'),
          ),
          const SizedBox(height: 20),
          ElevatedButton(
            onPressed: () {
              _showModalBottomSheet(context);
            },
            child: const Text('showModalBottomSheet'),
          ),
          const SizedBox(height: 20),
          ElevatedButton(
            onPressed: () {
              _showFlutterToast();
            },
            child: const Text('showFlutterToast'),
          ),
          const SizedBox(height: 20),
          ElevatedButton(
            onPressed: () {
              _showMyDialog();
            },
            child: const Text('showMyDialog'),
          ),
          const SizedBox(height: 20),
          ElevatedButton(
            onPressed: () {
              _showGetXDialog();
            },
            child: const Text('showGetXDialog'),
          ),
        ],
      ),
    );
  }
}
